//
//  BrandsViewController.m
//  IDailyWatch
//
//  Created by 老师先给我发 on 14-10-13.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "BrandsViewController.h"
#import "Mycell.h"
#import "Product.h"
#import "InBrandsController.h"

@interface BrandsViewController ()<UITableViewDataSource, UITableViewDelegate, MyCellDelegate>
@property (nonatomic , retain)NSMutableArray *arr;
@property (nonatomic , retain)NSMutableArray *arr1;
@property (nonatomic , retain)NSMutableArray *photoArr;
@property (nonatomic, retain)NSMutableArray *nameArr;
@property (nonatomic , retain)NSMutableDictionary *dic;
@property (nonatomic, retain)NSArray *Arraykey;
@property (nonatomic , retain)NSMutableArray *NewArryKey;
@property(nonatomic, assign)NSInteger count;
@property (nonatomic , retain)UITableView *tableVeiw;
@property (nonatomic, retain)Product *emptyModel;

@end

@implementation BrandsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.arr = [NSMutableArray array];
        self.arr1 = [NSMutableArray array];
        self.photoArr = [NSMutableArray array];
        self.nameArr = [NSMutableArray array];
        self.Arraykey = [NSArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.translucent = NO;
    
    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 100, 44)]; //设置Label背景透明
    titleLabel.font = [UIFont boldSystemFontOfSize:20];  //设置文本字体与大小
    titleLabel.textColor = [UIColor whiteColor];  //设置文本颜色
    titleLabel.textAlignment = 1;
    titleLabel.text = @"BRANDS";  //设置标题
    self.navigationItem.titleView = titleLabel;
    [self getDate];
}
- (void)createTableview

{
    self.myWidth = [UIScreen mainScreen].bounds.size.width;
    self.myHeight = [UIScreen mainScreen].bounds.size.height;
    
    
    self.tableVeiw = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _myWidth, _myHeight *  (568 - 44 - 49) / 568) style:UITableViewStylePlain];
    _tableVeiw.delegate = self;
    _tableVeiw.dataSource = self;
    [self.view addSubview: _tableVeiw];
    [_tableVeiw release];

}


-(void)getDate
{           
    
    self.dic = [NSMutableDictionary dictionary];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://watch-cdn.idailywatch.com/api/list/brands/zh-hans?page=1&ver=iphone&app_ver=9" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arr1 = [NSMutableArray arrayWithArray:responseObject];
        for (int i = 0; i < _arr1.count; i++) {
            Product *model = [[Product alloc]initWithDictionary:[_arr1 objectAtIndex:i]];
            [_arr addObject:model];
            
           
            
        }
        
        for (unichar i = 'A'; i <= 'Z'; i++)
        {
            self.nameArr = [NSMutableArray array];
            
            for (Product * model in _arr)
            {
                //如果mdel的首字母 和循环元素的首字母相等
        if ([[model.name substringToIndex:1] isEqualToString:[NSString stringWithFormat:@"%c",i]])
        {
            NSString *st = [NSString stringWithFormat:@"%@",model.photo];
            
            [_photoArr addObject:st];
                    
            [_nameArr addObject:model];
                    
                }
            }
           // if (_nameArr.count != 0) {
            
                [_dic setObject:_nameArr forKey:[NSString stringWithFormat:@"%c",i]];
            //}
            
            
            }

        
            self.Arraykey  = [_dic allKeys];
            self.NewArryKey = [NSMutableArray arrayWithArray:_Arraykey];
            NSArray *array = [self.Arraykey sortedArrayUsingSelector:@selector(compare:)];
            self.NewArryKey  = [NSMutableArray arrayWithArray:array];
            [self.tableVeiw reloadData];
        if (_arr.count != 0) {
            
            [self createTableview];
        }
        
        
    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"尚无网络" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
             [alertView show];
             [alertView release];
         }];
}


// 协议方法
- (void)pushViewController:(UIViewController *)viewController count:(NSInteger)count
{
    
    InBrandsController * vc = [[InBrandsController alloc]init];

    vc.modelArr = self.arr;
    vc.count = count;
    
    [self.navigationController pushViewController:vc animated:YES];
    
}




//tableView cell个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"111";
   Mycell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell  = [[[Mycell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
        cell.delegate = self;
    
    }
    
    
    cell.brandsArr2 = _arr;
    cell.brandsArr = [_dic objectForKey:[_NewArryKey objectAtIndex:indexPath.section]];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    return cell;
}

//高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    unichar i = indexPath.section + 65;
    NSString *key = [NSString stringWithFormat:@"%c",i];

    return  ([[_dic objectForKey:key]count] + 1)/2 * 80;
    
}


//头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}



//个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return _NewArryKey.count;
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    for (int i = 0; i <  _NewArryKey.count; i++) {
        if (section == i) {
            return [ _NewArryKey objectAtIndex:i];
        }
    }
    
    return 0;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = YES;
    
}-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
